<?php

namespace app\wechat\controller;

use app\wechat\validate\ReceiveValidate;
use think\Db;

class Receive extends Base
{
    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function NoReceive()
    {
        $orderInfo = model('order')->where('status', 3)->select();
        $viewData  = [
            'order' => $orderInfo
        ];
        $this->assign($viewData);
        return view();
    }

    /**
     * @return mixed
     */
    public function Receive()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $ret  = $this->validate($data, ReceiveValidate::class);
            if ($ret !== true) {
                $this->error($ret);
            }
            $data['status'] = 4;
            $user     = session("user");
            $data['receive_user']=$user['id'];
            $data['receive_time'] = time();
            $result         = model('order')
                ->save($data, ['order_number' => $data['order_num']]);
            if ($result) {
                $wechatMessage =  new Message();
                $order_Info = Db::name('order')->where('order_number',$data['order_num'])->find();
                $applyInfo = Db::name('users')->where('id',$order_Info['user_id'])->find();
                $wechatMessage->ReceiveToApply($applyInfo['openid'],$applyInfo['real_name'],$order_Info['goods']);
                $this->success('收货成功', 'wechat/index/index');
            } else {
                $this->error('收货失败');
            }
        }
        $orderInfo = model('order')
            ->where('order_number', input('id'))
            ->find();
        $priceInfo = model('price')
            ->where('id', $orderInfo['price_id'])
            ->find();
        $viewDate  = [
            'order' => $orderInfo,
            'price' => $priceInfo
        ];
        $this->assign($viewDate);
        return view();
    }

    //已经收货列表
    public function Received()
    {
        $orderInfo = model('order')
            ->where('status' ,'>', 3)
            ->select();
        $viewData  = [
            'order' => $orderInfo
        ];
        $this->assign($viewData);
        return view();
    }

    //
    public function details()
    {
        $orderInfo = model('order')
            ->where('order_number', input('id'))
            ->find();
        $priceInfo = model('price')
            ->where('id', $orderInfo['price_id'])
            ->find();
        $viewDate  = [
            'order' => $orderInfo,
            'price' => $priceInfo
        ];
        $this->assign($viewDate);
        return view();
    }
}
